package server

import (
	"fmt"

	"github.com/TarsCloud/TarsGo/tars"
	"github.com/TarsCloud/TarsGo/tars/util/conf"
	hypernexusclient "github.com/wzh200x/hyperNexus/hyperNexusClient"
	"github.com/wzh200x/hyperNexus/hyperNexusProtocol/dbAgentServerProtocol"
)

type DbAgentServer struct {
	GdbServerProxy   *hypernexusclient.GdbServerClient
	CacheServerProxy *hypernexusclient.CacheServerClient
}

func (ca *DbAgentServer) Init(comm *tars.Communicator, sConf *conf.Conf) error {
	ca.GdbServerProxy.Init(comm)
	ca.CacheServerProxy.Init(comm)
	ca.RegisterTarsObj()
	return nil
}

func (ca *DbAgentServer) InitFinish() error {
	return nil
}

func (ca *DbAgentServer) Info() string {
	return fmt.Sprintf(`server :%s version :%s`, "DbAgentServer", "0.0.1")
}

func (ca *DbAgentServer) Name() string {
	return "DbAgentServer"
}

func (ca *DbAgentServer) Stop() error {
	return nil
}

func (ca *DbAgentServer) RegisterTarsObj() {
	// Get server config
	cfg := tars.GetServerConfig()
	// New servant imp
	imp := new(dbAgnetServerOperateImp)
	// 初始化servant
	err := imp.Init()
	if err != nil {
		tars.GetLogger("").Errorf("初始化dbAgnetServerOperateImp失败: %v", err)
	}

	app := new(dbAgentServerProtocol.DbAgentServer)
	// Register Servant
	app.AddServantWithContext(imp, cfg.App+"."+cfg.Server+".DBAgentServerOperateObj")
}

func NewServer() *DbAgentServer {
	gs := &DbAgentServer{
		GdbServerProxy:   hypernexusclient.NewGdbServerClient(),
		CacheServerProxy: hypernexusclient.NewCacheServerClient(),
	}
	return gs
}

var ServerApp *DbAgentServer = NewServer()
